<template>
  <div>
    <div style="margin-top: 2%;text-align:center;">
      <el-button @click="addTerm()" style="font-size: 20px" type="primary">点击此处新增学期</el-button>
    </div>

    <template style="margin-top: 2%">
      <el-table :data="pageInfo.list" style="width: 100%">
        <el-table-column label="" width="160">
        </el-table-column>
        <el-table-column label="" width="240" prop="termId" sortable>
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{scope.row.termId}}</span>
          </template>
        </el-table-column>
        <el-table-column label="学期名称" width="1000" prop="termName" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.termName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="small"
                style="background-color: deepskyblue;font-size: 16px"
                @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-bottom: 1%;"></div>
      <el-pagination
          style="text-align: center;"
          background
          layout="total,prev, pager, next"
          :total="pageInfo.total"
          :page-size="pageInfo.pageSize"
          @current-change="handleCurrentChange">
      </el-pagination>
    </template>

    <el-drawer title="新增学期" :visible.sync="add" :direction="addDirection">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="30%">
        <el-form-item label="学期名称" prop="termName">
          <el-input v-model="ruleForm.termName" placeholder="例如:2018-2019 上学期" style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即新增</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <el-drawer title="修改学期" :visible.sync="modify" :direction="modifyDirection">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="30%">
        <el-form-item label="学期名称" prop="termName">
          <el-input v-model="ruleForm.termName" placeholder="例如:2018-2019 上学期" style="width: 60%"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      add: false,
      addDirection: 'btt',
      modify: false,
      modifyDirection: 'btt',
      currPage: 1,
      pageInfo:{},
      ruleForm:{
        termId: '',
        termName: '',
      },
      rules: {
        termName: [
          { required: true, message: '请输入学期名称', trigger: 'blur' },
          { min: 2, max: 50, message: '请输入 2 到 50 个字符!', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.getData();
  },
  methods: {
    submitForm(formName){ // 新增/修改学期
      this.$refs[formName].validate((valid) => {//校验格式
        if (valid) {//格式校验通过
          let path = 'http://localhost:8081/term/updateTerm';
          this.$ajax.post(path,this.ruleForm).then(res=>{
            if(res.data.status === 0){
              this.$message({
                showClose: true,
                message: res.data.message,
                type: 'success'
              });
              this.add = false;
              this.modify = false;
              this.getData();
            }
          })
        }
      });
    },
    getData(){
      let path = `http://localhost:8081/term/getTermList/${this.currPage}`;
      this.$ajax.post(path,this.ruleForm).then(res=>{
        this.pageInfo = res.data;
      })
    },
    handleCurrentChange(val){
      this.currPage = val;
      this.getData();
    },
    handleEdit(row){
      this.ruleForm.termId = row.termId;
      this.ruleForm.termName = row.termName;
      this.modify = true;
    },
    addTerm(){
      this.ruleForm.termId = '';
      this.ruleForm.termName = '';
      this.add = true;
    }
  }
};
</script>

<style scoped>
span{
  font-size: 18px;
}
</style>